package com.oim.fx.view;

import java.util.ArrayList;
import java.util.List;

import com.oim.core.app.AppContext;
import com.oim.core.business.controller.RemoteController;
import com.oim.core.business.sender.DeviceSender;
import com.oim.core.common.app.view.AbstractView;
import com.oim.core.net.message.data.DeviceData;
import com.oim.core.net.message.different.PageImpl;
import com.oim.core.net.server.Back;
import com.oim.function.ui.DeviceListFrame;
import com.oim.function.ui.device.DeviceItem;
import com.oim.fx.ui.component.WaitingPanel;
import com.only.common.result.Info;
import com.only.general.annotation.parameter.Define;
import com.only.net.data.action.DataBackActionAdapter;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.util.Callback;

/**
 * @author: XiaHui
 * @date: 2017年4月11日 下午12:07:41
 */
public class DeviceListViewImpl extends AbstractView implements DeviceListView {

	DeviceListFrame deviceListFrame = new DeviceListFrame();

	public DeviceListViewImpl(AppContext appContext) {
		super(appContext);
		initEvent();
	}

	private void initEvent() {
		DataBackActionAdapter dataBackAction = new DataBackActionAdapter() {

			@Override
			public void lost() {
				showWaiting(true, WaitingPanel.show_result);
			}

			@Override
			public void timeOut() {
				showWaiting(true, WaitingPanel.show_result);
			}

			@Back
			public void back(Info info, @Define("list") List<DeviceData> deviceList, @Define("page") PageImpl page) {
				setList(deviceList, page);
				showWaiting(false, WaitingPanel.show_waiting);
			}
		};

		deviceListFrame.setPage(0, 1);
		deviceListFrame.setPageFactory(new Callback<Integer, Node>() {

			@Override
			public Node call(Integer index) {

				int pageNumber = (index + 1);

				PageImpl page = new PageImpl();
				DeviceSender ds = appContext.getSender(DeviceSender.class);
				ds.queryDeviceList(page, dataBackAction);
				deviceListFrame.showWaiting(true, WaitingPanel.show_waiting);
				return new Label("第" + pageNumber + "页");
			}
		});
	}

	public void setList(List<DeviceData> deviceList, PageImpl page) {
		List<DeviceItem> list = new ArrayList<DeviceItem>();
		if (null != deviceList) {
			for (DeviceData dd : deviceList) {
				DeviceItem item = new DeviceItem();
				String status = dd.getStatus();
				boolean online = "1".equals(status);
				item.setNameLabel(dd.getName());
				item.setAccount(dd.getAccount());
				item.setSn(dd.getSn());
				item.setIP(dd.getIp());
				item.setStatusLabel(online ? "在线" : "离线");

				item.setOnAction(new EventHandler<ActionEvent>() {

					@Override
					public void handle(ActionEvent event) {
						if (online) {
							RemoteController rc = appContext.getController(RemoteController.class);
							rc.requestRemoteControl(dd.getUserId());
						} else {
							deviceListFrame.showPrompt("设备不在线！");
						}
					}
				});
				list.add(item);
			}
		}

		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				deviceListFrame.setNodeList(list);
				deviceListFrame.setTotalPage(page.getTotalPage());
			}
		});
	}

	@Override
	public void setVisible(boolean visible) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				if(!isShowing()){
					deviceListFrame.setPage(0, 1);
				}
				if (visible) {
					deviceListFrame.show();
					deviceListFrame.toFront();
				} else {
					deviceListFrame.hide();
				}
			}
		});
	}

	@Override
	public boolean isShowing() {
		return deviceListFrame.isShowing();
	}

	public void showWaiting(boolean show, String key) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				deviceListFrame.showWaiting(show, key);
			}
		});
	}
}
